import { getToken, setToken } from '@/utils/auth'
import { sysLogin, sysProfile, sysUser } from '@/api/user.js'

const state = {
  token: getToken() || '',
  userInfo: ''
}
const mutations = {
  setToken(state, value) {
    state.token = value
    setToken(value)
  },
  setUserInfo(state, value) {
    state.userInfo = value
  },
  // 退出
  logout(state, value) {
    state.token = ''
    setToken('')
    state.userInfo = ''
  }
}
const actions = {
  async toLogin(store, data) {
    const res = await sysLogin(data)
    store.commit('setToken', res.data)
    console.log('登录', res)
  },
  async getuserInfo({ commit }) {
    const res = await sysProfile()
    //  获取头像接口
    const res2 = await sysUser(res.data.userId)
    commit('setUserInfo', { ...res.data, ...res2.data })
    console.log(('用户信息', res, res2))
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
